import random
import sys

class Hangman:
    def __init__(self):
        self.secret_word = self.word_generation()
        self.guessed_letters = ""
        self.lives = 6

    def word_generation(self):
        words = "critter zomg noob lol creche trainer pokemon hacks awesome".split()
        return random.choice(words)

    def show_board(self):
        display = ""
        for letter in self.secret_word:
            if letter in self.guessed_letters:
                display += letter + " "
            else:
                display += "_ "
        print display
        if "_" not in display:
            print "You win!"
            self.play_again()

    def turn(self):
        guess = ""
        while len(guess) != 1: 
            print str(self.lives) + " guesses left."
            guess = raw_input("Guess a letter: ")
            if len(guess) != 1:
                print "only one letter, please!"
            elif guess in self.guessed_letters:
                print "Already guessed that!"
                guess = ""
            elif guess in self.secret_word:
                print "Yippie!\n"
            elif guess not in self.secret_word:
                print "Oh snap!\n"
                lives = self.hang()
        self.guessed_letters += guess

    def hang(self):
        self.lives -= 1
        if self.lives <= 0:
            print "You're dead! Word was " + self.secret_word
            self.play_again()

    def play_again(self):
        answer = raw_input("Play again? (y/n) ")
        self.__init__()
        if "y" in answer:
            self.lives = 6
        else:
            self.lives = 0

    def main(self):
        self.show_board()
        self.turn()

game = Hangman()

while game.lives > 0:
    game.main()
    
    
